/*
 * @Description: 选择排序算法实现
 * @Version: 
 * @Author: Weixin Zhu
 * @Date: 2020-11-25 13:58:02
 * @LastEditors: Weixin Zhu
 * @LastEditTime: 2020-12-11 16:47:57
 */
package com.zwx.algorithms.basic;

import com.zwx.algorithms.constant.AlgorithmsConstant;
import com.zwx.algorithms.pub.util.AlgorithmsUtils;
import com.zwx.algorithms.pub.util.LogUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;

public class SelectionSort extends AbstractArraySort {
    static final Logger log = LoggerFactory.getLogger(SelectionSort.class);

    /**
     * @description: 选择排序算法实现
     * @param {Integer[]}
     *                        array
     * @return {*}
     */
    public Integer[] sort(Integer[] array) {
        int size = array.length;
        for (int i = 0; i < array.length; i++) {
            int value = array[i];
            //
            int j = i + 1;
            while (j < size) {
                totalStep++;
                if (AlgorithmsUtils.needShowStep(size)) {
                    log.debug("index :{},i:{},j:{}", totalStep, i, j);
                }
                if (value > array[j]) {
                    array[i] = array[j];
                    array[j] = value;
                    value = array[i];
                }
                j = j + 1;
            }
        }

        return array;
    }

    public static void main(String[] args) {
        LogUtils.resetLogLevel(log, org.slf4j.event.Level.INFO);
        Integer[] originArray = null;
        Integer[] sortResult = null;
        AbstractArraySort sort = new SelectionSort();
        StopWatch sw = DateUtil.createStopWatch();
        originArray = AlgorithmsUtils.generateRandomArray(AlgorithmsConstant.MAX_VALUE);
        sw.start();
        sortResult = sort.sort(originArray);
        sw.stop();

        AlgorithmsUtils.printSortedArray(sortResult);
        log.info("size:{},totalStep:{},cost time:{}", sortResult.length, sort.getTotalStep(), sw.getLastTaskTimeMillis());
        log.info(sw.prettyPrint());
    }

}
